/*
public class TreeLinkNode {
    int val;
    TreeLinkNode left = null;
    TreeLinkNode right = null;
    TreeLinkNode next = null;

    TreeLinkNode(int val) {
        this.val = val;
    }
}
*/
import java.util.*;

public class Solution {

    ArrayList<TreeLinkNode> arr = new ArrayList<>();

    public void inorder(TreeLinkNode node) {
        if (node == null)
            return ;
        inorder(node.left);
        arr.add(node);
        inorder(node.right);
    }

    public TreeLinkNode GetNext(TreeLinkNode pNode) {
        if (pNode == null)
            return null;
        
        TreeLinkNode root = pNode;
        while (root.next != null) 
            root = root.next;

        inorder(root);

        for (int i = 0; i < arr.size() - 1; i++)
            if (arr.get(i) == pNode)
                return arr.get(i +1 );
        
        return null;
    }
}
